package DataLayer.ImageDataContainer.RetrievedText;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;

public class RetrievedText implements IRetrievedText {

    protected String retrievedText;

    public RetrievedText() {
        // TODO add implementation
    }

    /**
     * Sets the retrieved text with the text parameter
     * @param text - The text.
     */
    public void setRetrievedText(String text) {
        retrievedText = text;
    }

    /**
     * Returns the retrieved text.
     * @return - The retrieved text.
     */
    public String getRetrievedText() {
        return retrievedText;
    }

    /**
     * Generates a PDF document from the recognized text.
     * @param text - The text to be written in the PDF.
     * @param nameOfPDF - The path and name of the desired PDF document.
     */
    public void generatePDF(String nameOfPDF) {
        try {
            if(!nameOfPDF.endsWith(".pdf")){
                nameOfPDF +=".pdf";
            }
            Document document = new Document(PageSize.A4, 36, 72, 108, 180);
            PdfWriter.getInstance(document, new FileOutputStream(nameOfPDF));
            document.open();
            document.add(new Paragraph(retrievedText));
            ////System.out.println("Text is inserted into pdf file: " + nameOfPDF + "text: " + retrievedText);
            document.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}
